<?php


namespace App\Http\OS\Controllers;

use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
use OpenSearch\Client\AppClient;
use OpenSearch\Generated\Common\Pageable;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class IndexController extends Controller{

    public function select_title($title){

        $client = OSclient();

        $searchClient = new SearchClient($client);
        // 实例化一个搜索参数类
        $params = new SearchParamsBuilder();
//设置config子句的start值
        $params->setStart(0);
//设置config子句的hit值
        $params->setHits(20);
// 指定一个应用用于搜索
        $params->setAppName('jiali_smart');
// 指定搜索关键词
        $params->setQuery("title:'{$title}'");
// 指定返回的搜索结果的格式为json
        $params->setFormat("fulljson");

        $params->addSummary(
                array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );

        $params->addSummary(
                array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );

        $params->addSummary(
                array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );

        $params->setFetchFields(array('id','title','case_action','case_number','case_time','case_type','subject','content'));



//添加排序字段
        $params->addSort('id', SearchParamsBuilder::SORT_DECREASE);
// 执行搜索，获取搜索结果
        $ret = $searchClient->execute($params->build());
// 将json类型字符串解码
        $res = json_decode($ret->result,true)['result']['items'];
//打印调试信息
        $ret->traceInfo->tracer;

        $newres = [];

        foreach($res as $k=>$v){
                $newres[]=$v['fields'];
        }

        return $newres;




    }

    public  function select_content($content){

        $client = OSclient();

        $searchClient = new SearchClient($client);
        // 实例化一个搜索参数类
        $params = new SearchParamsBuilder();
//设置config子句的start值
        $params->setStart(0);
//设置config子句的hit值
        $params->setHits(20);
// 指定一个应用用于搜索
        $params->setAppName('jiali_smart');
// 指定搜索关键词
        $params->setQuery("default:'{$content}'");
// 指定返回的搜索结果的格式为json
        $params->setFormat("fulljson");

        $params->addSummary(
                array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );

        $params->addSummary(
                array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );

        $params->addSummary(
                array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );

        $params->setFetchFields(array('id','title','case_action','case_number','case_time','case_type','subject','content'));




//添加排序字段
        $params->addSort('id', SearchParamsBuilder::SORT_DECREASE);
// 执行搜索，获取搜索结果
        $ret = $searchClient->execute($params->build());
// 将json类型字符串解码
$res = json_decode($ret->result,true)['result']['items'];
//打印调试信息
        $ret->traceInfo->tracer;

        $newres = [];

        foreach($res as $k=>$v){
                $newres[]=$v['fields'];
        }

        return $newres;



    }



}
